#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include "client.h"

int log_in (const char *name, const char *passwd)
{
	int tmp, tmp2 = -1;

	tmp = LOG_IN;
	send(sock_fd, &tmp, sizeof(int), 0);

	tmp = strlen(name);
	send(sock_fd, &tmp, sizeof(int), 0);
	send(sock_fd, name, strlen(name), 0);

	tmp = strlen(passwd);
	send(sock_fd, &tmp, sizeof(int), 0);
	send(sock_fd, passwd, strlen(passwd), 0);

	recv(sock_fd, &tmp, sizeof(int), 0);

	if ( tmp == 0 )
	{
		memset(log_in_as, 0, NAME_LEN_MAX + 1);
		strcpy(log_in_as, name);
		tmp2 = log_in2(log_in_as);
	}

	return tmp2;
}

int log_in2 (const char *name)
{
	int tmp;
	tmp = LOG_IN2;
	send(sock_fd2, &tmp, sizeof(int), 0);

	tmp = strlen(name);
	send(sock_fd2, &tmp, sizeof(int), 0);
	send(sock_fd2, name, tmp, 0);

	recv(sock_fd2, &tmp, sizeof(int), 0);

	return tmp;
}
#ifdef DEBUG
int main (int argc, char* argv[])
{
	if ( argc != 3)
	{
		printf ("usage: log_in username password\n");
		exit (EXIT_FAILURE);
	}
	net_init();
	printf("res:\t%d\n", log_in(argv[1], argv[2]));
}
#endif
